<template>

  <div>

    <data-table
      ref="pagingTable"
      :options="options"
      :list-query="listQuery"
      @add="showAdd"
      @edit="showUpdate"
      @multi-actions="handleMultiAction"
    >

      <template #filter-content>

        <el-input
          v-model="listQuery.params.title"
          style="width: 200px"
          placeholder="搜索公告标题"
          class="filter-item"
          size="small"
        />

      </template>

      <template #data-columns>

        <el-table-column
          type="selection"
          width="55"
        />

        <el-table-column
          label="标题"
          prop="title"
        >

          <template v-slot="{ row }">
            <detail-link :id="row.id" :title="row.title" permission="sys:notice:update" @click="showUpdate(row.id)" />
          </template>

        </el-table-column>

        <el-table-column
          align="center"
          label="发布时间"
          prop="createTime"
        />

        <el-table-column
          align="center"
          label="置顶"
        >

          <template v-slot="{ row }">
            <span v-if="row.topLevel > 0" style="color: #ff3403">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="状态"
        >

          <template v-slot="{ row }">
            {{ row.state | stateFilter }}
          </template>
        </el-table-column>

      </template>
    </data-table>

    <notice-save-dialog :visible.sync="dialogVisible" :id="noticeId" @saved="refreshTable"/>
  </div>

</template>

<script>
import NoticeSaveDialog from '@/views/admin/sys/notice/components/NoticeSaveDialog.vue'
import { markTop } from '@/api/sys/notice/notice'

export default {
  components: { NoticeSaveDialog },
  data() {
    return {
      dialogVisible: false,
      listQuery: {
        current: 1,
        size: 10,
        params: {
        }
      },
      options: {
        add: {
          enable: true,
          permission: 'sys:notice:add'
        },
        edit: {
          enable: true,
          permission: 'sys:notice:update'
        },
        delete: {
          enable: true,
          permission: 'sys:notice:delete',
          url: '/api/sys/notice/delete'
        },
        // 列表请求URL
        listUrl: '/api/sys/notice/paging',
        // 批量操作列表
        multiActions: [
          {
            value: 'enable',
            label: '启用',
            permission: 'sys:notice:update',
            url: '/api/sys/notice/state'
          }, {
            value: 'disable',
            label: '禁用',
            permission: 'sys:notice:update',
            url: '/api/sys/notice/state'
          },
          {
            value: 'mark-top',
            label: '置顶',
            permission: 'sys:notice:update'
          }, {
            value: 'cancel-top',
            label: '取消置顶',
            permission: 'sys:notice:update'
          }
        ]
      },
      noticeId: ''
    }
  },

  methods: {
    // 刷新列表
    refreshTable() {
      this.noticeId = ''
      this.$refs.pagingTable.refresh()
    },

    showAdd() {
      this.noticeId = ''
      this.dialogVisible = true
    },

    showUpdate(id) {
      this.noticeId = id
      this.dialogVisible = true
    },

    handleMultiAction(obj) {
      const params = { ids: obj.ids, top: obj.opt === 'mark-top' }
      markTop(params).then(() => {
        this.$message.success('操作成功！')
        this.$refs.pagingTable.getList()
      })
    }
  }
}
</script>

<style scoped>

  .el-dialog__body{
    padding: 0 20px;
  }

  ::v-deep .el-dialog__wrapper{
    z-index: 0;
  }

</style>
